const http = require("http");
const fs = require("fs");
const url = require("url");
const path = require("path");
let mimeData = require("./mime.json");
const server = http.createServer((req, res) => {
    console.log(req.url);

    var urlRes = url.parse(req.url, true);
    console.log(urlRes);

    let pathname = urlRes.pathname;

    res.setHeader("Content-Type", "text/html;charest=utf-8");
    if (pathname == "/index") {
        let data = fs.readFileSync("./views/index.html");
        res.write(data);
        res.end();
    } else if (pathname == "/product") {
        let data = fs.readdirSync("./views/product.html");
        res.write(data);
        res.end();
    } else {
        //第三方资源
        if (req.rul != "/favicon.ico") {
            let extname = path.extname(req.url);
            let contenttype = mimeData[extname];
            console.log(extname);
            res.setHeader("Content-Type", contenttype);
            let data = fs.readFileSyne("./static/" + req.url);
            res.write(data);
            res.end();
        }
    }
})

server.listen(3456);